﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using ComicQ.Domain;
using ComicQ.Domain.Table;
using NHibernate.Linq;

namespace ComicQ.Dektop.View
{
    /// <summary>
    /// Interaction logic for MemberView.xaml
    /// </summary>
    public partial class MemberView
    {
        private IList<Member> _members;

        public MemberView()
        {
            InitializeComponent();

            using (var session = NHibernateHelper.OpenSession())
            {
                _members = session.Query<Member>().ToList();
            }

            dataGridMember.ItemsSource = _members;
        }

        private void ButtonAddClick(object sender, RoutedEventArgs e)
        {
            var newWindow = new MemberEditorView();

            if (newWindow.ShowDialog() != false)
            {
            }
            else
            {
                dataGridMember.ItemsSource = null;

                using (var session = NHibernateHelper.OpenSession())
                {
                    _members = session.Query<Member>().ToList();
                }

                dataGridMember.ItemsSource = _members;
            }
        }

        private void ButtonDeleteClick(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dataGridMember.SelectedItem == null)
                    throw new NullReferenceException();

                using (var session = NHibernateHelper.OpenSession())
                {
                    var member = session.Query<Member>()
                        .Where(m => m.Id == _members[dataGridMember.SelectedIndex].Id)
                        .Select(m => m)
                        .Single();

                    using (var transaction = session.BeginTransaction())
                    {
                        session.Delete(member);
                        transaction.Commit();
                        MessageBox.Show("Delete sukses.");
                    }
                }

                dataGridMember.ItemsSource = null;

                using (var session = NHibernateHelper.OpenSession())
                {
                    _members = session.Query<Member>().ToList();
                }

                dataGridMember.ItemsSource = _members;
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("Pilih yang mau didelete.");
            }
        }

        private void ButtonEditClick(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dataGridMember.SelectedItem == null)
                    throw new NullReferenceException();

                var newWindow = new MemberEditorView(_members[dataGridMember.SelectedIndex]);

                if (newWindow.ShowDialog() != false)
                {
                }
                else
                {
                    dataGridMember.ItemsSource = null;

                    using (var session = NHibernateHelper.OpenSession())
                    {
                        _members = session.Query<Member>().ToList();
                    }

                    dataGridMember.ItemsSource = _members;
                }
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("Pilih yang mau diedit.");
            }
        }
    }
}
